import axios from 'axios/index'
import {
	message
} from 'ant-design-vue';
import store from '../store/index.js'
import moment from 'moment'

switch (process.env.NODE_ENV) {
	// case "development":
	//   /* axios.defaults.baseURL = "http://new.hfhp.com"*/
	//   axios.defaults.baseURL = "https://731056z4l.oicp.vip"
	//   break
	// case "build":
	//   axios.defaults.baseURL = "https://731056z4l.oicp.vip"
	//   break;
	// case "production":
	//   axios.defaults.baseURL = "https://731056z4l.oicp.vip"
	//   break;
	// case "test":
	//   axios.defaults.baseURL = "https://731056z4l.oicp.vip"
	//   break;
	// default:
	//   axios.defaults.baseURL = "https://731056z4l.oicp.vip"
	//   break;
}

/*import '../mock/api'*/
axios.defaults.baseURL = '/api'
// mock
// const mock = true
// if(mock){
//     axios.defaults.baseURL = '/api',
//     require('../mock/api.js')
// }

// import '../mock/api.js'

// 超时时间为8秒
axios.defaults.timeout = 800000

// 设置超时时间和跨域是否允许携带凭证
/*axios.defaults.withCredentials = true*/

// 设置请求传递数把格式
axios.defaults['Content-Type'] = 'application/json'

// 设置请求拦截器
axios.interceptors.request.use(config => {
	let token = store.state.token
	token && (config.headers.token = token)
	return config
}, error => {
	return Promise.reject(error)
})

// 响应拦截器
axios.interceptors.response.use(response => {
	let res = response.data;
	if (response.data.status === -1) {
		window.location.href = '/login?chang=5';
		return
	}
	if (response.data.status != 1) {
		message.error(response.data.message)
	}
	return res;
	if (res.status == 10) {
		// 未登录，10为后端给的状态码
		if (path != '#/index') {
			window.location.href = '/login';
		}
	} else {
		console.log(res.message)
	}
}, error => {
	let {
		response
	} = error
	// let response = error
	if (response) {
		// 服务器最起码返回结果了
		message.error(response.data.message)
	} else {
		// 服务器连结果都没有返回
		if (!window.navigator.onLine) {
			// 客户端断网处理
		}
		return Promise.reject(error)
	}
})

export default axios

/**
 *  @description 封装get请求
 *  @params: Object
 *  @return {Promise}
 * */
export function get(url, params = {}) {
	return new Promise((resolve, reject) => {
		axios.get(url, params)
			.then(response => {
				resolve(response)
			})
			.catch(err => {
				reject(err)
			})
	})
}

/**
 *  @description 封装post请求
 *  @params: Object
 *  @return {Promise}
 * */
export function post(url, params = {}) {
	return new Promise((resolve, reject) => {
		axios.post(url, params)
			.then(response => {
				resolve(response)
			}).catch(error => {
				reject(error)
			})
	})
}

/**
 *  @description 导出文件
 *  @params: Object
 *  @return {Promise}
 * */
export function downPost(url, params = {}) {
	return new Promise((resolve, reject) => {
		axios.post(url, params, {
			responseType: "blob"
		}).then(res => {
			const year = moment().format("YYYY-MM-DD")
			let reader = new FileReader()
			reader.readAsDataURL(res)
			reader.onload = (e) => {
				let a = document.createElement("a")
				a.download = `XD-${params.customName}-${year}.xlsx`
				a.href = e.target.result
				document.body.appendChild(a)
				a.click()
				document.body.removeChild(a)
			}
			resolve(res)
		}).catch(err => {
			reject(err)
		})
	})
}